package com.ksl.kinglockguard.widget.common;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ksl.kinglockguard.R;
import com.ksl.kinglockguard.utils.KingsSystemUtils;
import com.lsk.open.core.MyLog;

/*import com.kingsgame.gamecenter.Constants;
import com.kingsgame.gamecenter.R;
import com.kingsgame.gamecenter.data.MarketUpdateInfo;
import com.kingsgame.gamecenter.util.Util;*/

public class UpdateDialog extends Dialog {

	/**
	 * 更新类型
	 */
	public static final int UPDATE_NORMAL_CODE = 1; // 普通更新
	public static final int UPDATE_REC_CODE = 2; // 推荐更新
	public static final int UPDATE_FORCE_CODE = 3; // 强更
	
	private TextView forceTip;
	private TextView tv_new_ver_tip;
	private TextView txtVersion;
	private TextView txtSize;
	private TextView txtContent;
	private Context con;
	private Button btnOk;
	private Button btnLater;
	private LinearLayout btn_cancel_container;
	private int updateType;
	private boolean isForceUpdate = false;	//是否是静默更新，如果是静默更新，则确认按钮显示立即安装

	public UpdateDialog(Context context, int updateType) {
		super(context, R.style.lock_dialog);
		this.con = context;
		this.updateType = updateType;
		setContentView(R.layout.lock_dialog_app_update);
		forceTip = (TextView) findViewById(R.id.txt_update_force_tip);
		tv_new_ver_tip = (TextView) findViewById(R.id.tv_new_ver_tip);
		txtVersion = (TextView) findViewById(R.id.txt_update_version);
		txtSize = (TextView) findViewById(R.id.txt_update_size);
		txtContent = (TextView) findViewById(R.id.txt_update_content);
		btnOk = (Button) findViewById(R.id.btn_update);
		btnLater = (Button) findViewById(R.id.btn_later);
		btn_cancel_container = (LinearLayout) findViewById(R.id.btn_cancel_container);
		
		setUpdateType(updateType);
		this.setCanceledOnTouchOutside(false);
	}
	
	public void setUpdateInfo(String name, long length, String describe){
		txtVersion.setText("版本："+ name);
		txtSize.setText("大小："+ KingsSystemUtils.dataFormat(length));
		txtContent.setText(describe.replace("|","\n"));
	}
	
	public void setUpdateType(int updateType) {
		switch (updateType) {
			case UPDATE_FORCE_CODE:
				isForceUpdate = true;
				forceTip.setVisibility(View.VISIBLE);
				btnLater.setVisibility(View.GONE);
				btn_cancel_container.setVisibility(View.GONE);
				
				break;
			case UPDATE_REC_CODE:
				btnLater.setVisibility(View.GONE);
				btn_cancel_container.setVisibility(View.VISIBLE);
				break;
			case UPDATE_NORMAL_CODE:
				btnLater.setVisibility(View.VISIBLE);
				btn_cancel_container.setVisibility(View.VISIBLE);
				break;
			default:
				btnLater.setVisibility(View.VISIBLE);
				break;
		}
	}
	/**
	 * 设置更新标题
	 * @param title
	 */
	public void setNewVerTip(String title){
		tv_new_ver_tip.setText(title);
	}
	
	public void setSilentUpdateMode(Context context,boolean isSlient){
		if(isSlient){
			btnLater.setVisibility(View.GONE);
			tv_new_ver_tip.setText(context.getResources().getString(R.string.lock_dialog_update_tip_downloaded));
			btnOk.setText(context.getResources().getString(R.string.lock_dialog_update_install));
		}else{
			if(isForceUpdate){
				btnLater.setVisibility(View.GONE);
			}else{
				btnLater.setVisibility(View.VISIBLE);
			}
			tv_new_ver_tip.setText(context.getResources().getString(R.string.lock_dialog_update_tip1));
			btnOk.setText(context.getResources().getString(R.string.lock_dialog_update_btn_up));
		}
	}
	public void setisForceUpdateMode(Context context,boolean isForce){
		if(isForce){
			btnLater.setVisibility(View.GONE);
		}else{
			btnLater.setVisibility(View.VISIBLE);
		}
	}

	public void setBtnOkListener(View.OnClickListener onclick) {
		btnOk.setOnClickListener(onclick);
		dismiss();
	}
	
	public void setBtnLaterListener(View.OnClickListener onclick) {
		btnLater.setOnClickListener(onclick);
	}

	public void setBtnCancleListener(View.OnClickListener onclick) {
		MyLog.d("lijc0807","-------setBtnCancleListener ------");
		btn_cancel_container.setOnClickListener(onclick);
	}

}
